home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / net / raw_usrreq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  9.8 KB  |  398 lines

  1. RCS_ID_C="$Id: raw_usrreq.c,v 1.11 1993/06/04 11:16:15 jraja Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * raw_usrreq.c --- Raw Socket usrreq, initialization &c
  8.  *
  9.  * Last modified: Fri Jun  4 00:39:26 1993 jraja
  10.  *
  11.  * HISTORY
  12.  * $Log: raw_usrreq.c,v $
  13.  * Revision 1.11  1993/06/04  11:16:15  jraja
  14.  * Fixes for first public release.
  15.  *
  16.  * Revision 1.10  1993/05/16  21:09:43  ppessi
  17.  * RCS version changed.
  18.  *
  19.  * Revision 1.9  1993/04/13  22:20:45  jraja
  20.  * Removed one register keyword.
  21.  *
  22.  * Revision 1.8  93/04/11  22:18:37  22:18:37  jraja (Jarno Tapio Rajahalme)
  23.  * Changed function prototypes to be compatible with struct domain.
  24.  * 
  25.  * Revision 1.7  93/04/06  08:54:14  08:54:14  jraja (Jarno Tapio Rajahalme)
  26.  * Changed bcopy's to aligned_bcopy[_const] when appropriate.
  27.  * 
  28.  * Revision 1.6  93/04/05  17:46:17  17:46:17  jraja (Jarno Tapio Rajahalme)
  29.  * Changed spl storage variables to spl_t.
  30.  * Changed every .c file to use conf.h.
  31.  * 
  32.  * Revision 1.5  93/03/12  22:56:26  22:56:26  ppessi (Pekka Pessi)
  33.  * Moved input queue here.
  34.  * 
  35.  * Revision 1.4  93/03/05  03:12:49  03:12:49  ppessi (Pekka Pessi)
  36.  * Compiles with SASC. Initial test version
  37.  * 
  38.  * Revision 1.3  93/02/25  19:52:30  19:52:30  ppessi (Pekka Pessi)
  39.  *  Added prototypes
  40.  * 
  41.  * Revision 1.2  92/11/20  14:43:30  14:43:30  jraja (Jarno Tapio Rajahalme)
  42.  * added #ifndef AMITCP's to get this compile somehow
  43.  * 
  44.  * Revision 1.1  92/11/20  13:32:37  13:32:37  jraja (Jarno Tapio Rajahalme)
  45.  * Initial revision
  46.  */
  47.  
  48. /* 
  49.  * Mach Operating System
  50.  * Copyright (c) 1992 Carnegie Mellon University
  51.  * All Rights Reserved.
  52.  * 
  53.  * Permission to use, copy, modify and distribute this software and its
  54.  * documentation is hereby granted, provided that both the copyright
  55.  * notice and this permission notice appear in all copies of the
  56.  * software, derivative works or modified versions, and any portions
  57.  * thereof, and that both notices appear in supporting documentation.
  58.  * 
  59.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  60.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  61.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  62.  * 
  63.  * Carnegie Mellon requests users of this software to return to
  64.  * 
  65.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  66.  *  School of Computer Science
  67.  *  Carnegie Mellon University
  68.  *  Pittsburgh PA 15213-3890
  69.  * 
  70.  * any improvements or extensions that they make and grant Carnegie Mellon 
  71.  * the rights to redistribute these changes.
  72.  */
  73. /*
  74.  * HISTORY
  75.  * Log:    raw_usrreq.c,v
  76.  * Revision 2.1  92/04/21  17:14:04  rwd
  77.  * BSDSS
  78.  * 
  79.  *
  80.  */
  81.  
  82. /*
  83.  * Copyright (c) 1980, 1986 Regents of the University of California.
  84.  * All rights reserved.
  85.  *
  86.  * Redistribution and use in source and binary forms, with or without
  87.  * modification, are permitted provided that the following conditions
  88.  * are met:
  89.  * 1. Redistributions of source code must retain the above copyright
  90.  *    notice, this list of conditions and the following disclaimer.
  91.  * 2. Redistributions in binary form must reproduce the above copyright
  92.  *    notice, this list of conditions and the following disclaimer in the
  93.  *    documentation and/or other materials provided with the distribution.
  94.  * 3. All advertising materials mentioning features or use of this software
  95.  *    must display the following acknowledgement:
  96.  *    This product includes software developed by the University of
  97.  *    California, Berkeley and its contributors.
  98.  * 4. Neither the name of the University nor the names of its contributors
  99.  *    may be used to endorse or promote products derived from this software
  100.  *    without specific prior written permission.
  101.  *
  102.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  103.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  104.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  105.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  106.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  107.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  108.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  109.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  110.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  111.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  112.  * SUCH DAMAGE.
  113.  *
  114.  *    @(#)raw_usrreq.c    7.9 (Berkeley) 6/28/90
  115.  */
  116.  
  117. #include <conf.h>
  118.  
  119. #include <sys/param.h>
  120. #include <sys/malloc.h>
  121. #include <sys/mbuf.h>
  122. #include <sys/domain.h>
  123. #include <sys/protosw.h>
  124. #include <sys/socket.h>
  125. #include <sys/socketvar.h>
  126. #include <sys/errno.h>
  127. #include <sys/systm.h>
  128.  
  129. #include <net/if.h>
  130. #include <net/route.h>
  131. #include <net/netisr.h> 
  132. #include <net/raw_cb.h>
  133.  
  134. #include <net/raw_usrreq_protos.h>
  135. #include <net/raw_cb_protos.h>
  136. #include <kern/uipc_socket_protos.h>
  137. #include <kern/uipc_socket2_protos.h>
  138.  
  139. struct    ifqueue rawintrq = {0};
  140.  
  141. /*
  142.  * Initialize raw connection block q.
  143.  */
  144. void 
  145. raw_init(void)
  146. {
  147.     rawcb.rcb_next = rawcb.rcb_prev = &rawcb;
  148.     rawintrq.ifq_maxlen = IFQ_MAXLEN;
  149. }
  150.  
  151.  
  152. /*
  153.  * Raw protocol input routine.  Find the socket
  154.  * associated with the packet(s) and move them over.  If
  155.  * nothing exists for this packet, drop it.
  156.  */
  157. /*
  158.  * Raw protocol interface.
  159.  */
  160.  
  161. int STKARGFUN
  162. raw_input(struct mbuf *m0,
  163.       struct sockproto *proto,
  164.       struct sockaddr *src,
  165.       struct sockaddr *dst)
  166. {
  167.     register struct rawcb *rp;
  168.     register struct mbuf *m = m0;
  169.     register int sockets = 0;
  170.     struct socket *last;
  171.  
  172.     last = 0;
  173.     for (rp = rawcb.rcb_next; rp != &rawcb; rp = rp->rcb_next) {
  174.         if (rp->rcb_proto.sp_family != proto->sp_family)
  175.             continue;
  176.         if (rp->rcb_proto.sp_protocol  &&
  177.             rp->rcb_proto.sp_protocol != proto->sp_protocol)
  178.             continue;
  179.         /*
  180.          * We assume the lower level routines have
  181.          * placed the address in a canonical format
  182.          * suitable for a structure comparison.
  183.          *
  184.          * Note that if the lengths are not the same
  185.          * the comparison will fail at the first byte.
  186.          */
  187. #define    equal(a1, a2) \
  188.   (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
  189.         if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
  190.             continue;
  191.         if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
  192.             continue;
  193.         if (last) {
  194.             struct mbuf *n;
  195.             if (n = m_copy(m, 0, (int)M_COPYALL)) {
  196.                 if (sbappendaddr(&last->so_rcv, src,
  197.                     n, (struct mbuf *)0) == 0)
  198.                     /* should notify about lost packet */
  199.                     m_freem(n);
  200.                 else {
  201.                     sorwakeup(last);
  202.                     sockets++;
  203.                 }
  204.             }
  205.         }
  206.         last = rp->rcb_socket;
  207.     }
  208.     if (last) {
  209.         if (sbappendaddr(&last->so_rcv, src,
  210.             m, (struct mbuf *)0) == 0)
  211.             m_freem(m);
  212.         else {
  213.             sorwakeup(last);
  214.             sockets++;
  215.         }
  216.     } else
  217.         m_freem(m);
  218.     return (sockets);
  219. }
  220.  
  221. void
  222. raw_ctlinput(int cmd, struct sockaddr *arg, caddr_t arg2)
  223. {
  224.     if (cmd < 0 || cmd > PRC_NCMDS)
  225.         return;
  226.     /* INCOMPLETE */
  227. }
  228.  
  229. int
  230. raw_usrreq(struct socket *so,
  231.        int req,
  232.        struct mbuf *m,
  233.        struct mbuf *nam,
  234.        struct mbuf *control)
  235. {
  236.     register struct rawcb *rp = sotorawcb(so);
  237.     register int error = 0;
  238.     int len;
  239.  
  240.     if (req == PRU_CONTROL)
  241.         return (EOPNOTSUPP);
  242.     if (control && control->m_len) {
  243.         error = EOPNOTSUPP;
  244.         goto release;
  245.     }
  246.     if (rp == 0) {
  247.         error = EINVAL;
  248.         goto release;
  249.     }
  250.     switch (req) {
  251.  
  252.     /*
  253.      * Allocate a raw control block and fill in the
  254.      * necessary info to allow packets to be routed to
  255.      * the appropriate raw interface routine.
  256.      */
  257.     case PRU_ATTACH:
  258.         if ((so->so_state & SS_PRIV) == 0) {
  259.             error = EACCES;
  260.             break;
  261.         }
  262.         error = raw_attach(so, (int)nam);
  263.         break;
  264.  
  265.     /*
  266.      * Destroy state just before socket deallocation.
  267.      * Flush data or not depending on the options.
  268.      */
  269.     case PRU_DETACH:
  270.         if (rp == 0) {
  271.             error = ENOTCONN;
  272.             break;
  273.         }
  274.         raw_detach(rp);
  275.         break;
  276.  
  277. #ifdef notdef
  278.     /*
  279.      * If a socket isn't bound to a single address,
  280.      * the raw input routine will hand it anything
  281.      * within that protocol family (assuming there's
  282.      * nothing else around it should go to). 
  283.      */
  284.     case PRU_CONNECT:
  285.         if (rp->rcb_faddr) {
  286.             error = EISCONN;
  287.             break;
  288.         }
  289.         nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
  290.         rp->rcb_faddr = mtod(nam, struct sockaddr *);
  291.         soisconnected(so);
  292.         break;
  293.  
  294.     case PRU_BIND:
  295.         if (rp->rcb_laddr) {
  296.             error = EINVAL;            /* XXX */
  297.             break;
  298.         }
  299.         error = raw_bind(so, nam);
  300.         break;
  301. #endif
  302.  
  303.     case PRU_CONNECT2:
  304.         error = EOPNOTSUPP;
  305.         goto release;
  306.  
  307.     case PRU_DISCONNECT:
  308.         if (rp->rcb_faddr == 0) {
  309.             error = ENOTCONN;
  310.             break;
  311.         }
  312.         raw_disconnect(rp);
  313.         soisdisconnected(so);
  314.         break;
  315.  
  316.     /*
  317.      * Mark the connection as being incapable of further input.
  318.      */
  319.     case PRU_SHUTDOWN:
  320.         socantsendmore(so);
  321.         break;
  322.  
  323.     /*
  324.      * Ship a packet out.  The appropriate raw output
  325.      * routine handles any massaging necessary.
  326.      */
  327.     case PRU_SEND:
  328.         if (nam) {
  329.             if (rp->rcb_faddr) {
  330.                 error = EISCONN;
  331.                 break;
  332.             }
  333.             rp->rcb_faddr = mtod(nam, struct sockaddr *);
  334.         } else if (rp->rcb_faddr == 0) {
  335.             error = ENOTCONN;
  336.             break;
  337.         }
  338.         error = (*so->so_proto->pr_output)(m, so);
  339.         m = NULL;
  340.         if (nam)
  341.             rp->rcb_faddr = 0;
  342.         break;
  343.  
  344.     case PRU_ABORT:
  345.         raw_disconnect(rp);
  346.         sofree(so);
  347.         soisdisconnected(so);
  348.         break;
  349.  
  350.     case PRU_SENSE:
  351.         /*
  352.          * stat: don't bother with a blocksize.
  353.          */
  354.         return (0);
  355.  
  356.     /*
  357.      * Not supported.
  358.      */
  359.     case PRU_RCVOOB:
  360.     case PRU_RCVD:
  361.         return(EOPNOTSUPP);
  362.  
  363.     case PRU_LISTEN:
  364.     case PRU_ACCEPT:
  365.     case PRU_SENDOOB:
  366.         error = EOPNOTSUPP;
  367.         break;
  368.  
  369.     case PRU_SOCKADDR:
  370.         if (rp->rcb_laddr == 0) {
  371.             error = EINVAL;
  372.             break;
  373.         }
  374.         len = rp->rcb_laddr->sa_len;
  375.         aligned_bcopy((caddr_t)rp->rcb_laddr, mtod(nam, caddr_t), (unsigned)len);
  376.         nam->m_len = len;
  377.         break;
  378.  
  379.     case PRU_PEERADDR:
  380.         if (rp->rcb_faddr == 0) {
  381.             error = ENOTCONN;
  382.             break;
  383.         }
  384.         len = rp->rcb_faddr->sa_len;
  385.         aligned_bcopy((caddr_t)rp->rcb_faddr, mtod(nam, caddr_t), (unsigned)len);
  386.         nam->m_len = len;
  387.         break;
  388.  
  389.     default:
  390.         panic("raw_usrreq");
  391.     }
  392. release:
  393.     if (m != NULL)
  394.         m_freem(m);
  395.     return (error);
  396. }
  397.  
  398.